package com.shujia.flink.kafka

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object Demo1Produce {
  def main(args: Array[String]): Unit = {


    val properties: Properties = new Properties

    //咨询参数配置
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
    //key value序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    properties.setProperty("transactional.id", "1")

    val prducer: KafkaProducer[String, String] = new KafkaProducer[String, String](properties)


    //开启事务

    prducer.initTransactions()
    prducer.beginTransaction()

    //构建一行数据
    val java: ProducerRecord[String, String] = new ProducerRecord[String, String]("trans", "java")

    //发送数据到kafka
    prducer.send(java)
    prducer.flush()


//    println(100 / 0)

    Thread.sleep(2000)

    val spark: ProducerRecord[String, String] = new ProducerRecord[String, String]("trans", "spark")


    prducer.send(spark)

    prducer.flush()

    //提交事务
    prducer.commitTransaction()

    prducer.close()

  }
}
